from pymongo import MongoClient
import pymongo
import os
import json
import csv
client = MongoClient(port=27017)
db=client['tiktok']
labels={}
with open('D:\\Work\\Tool\\tiktok\\all\\infos.tsv', 'r', encoding='utf-8',
          newline='\n') as filename_input:
    reader = csv.reader(filename_input, delimiter='\t')
    next(reader)
    for line in reader:
        try:
            labels[line[1]]=line[17]
        except:
            print(line[1])

for col in db.list_collection_names():
    print('updating ',col)
    for item in db[col].find():
        if 'video_len' in item['video_feature']['editing'].keys():
            continue
        if item['_id'] not in labels.keys():
            continue
        vl=int(labels[item['_id']])
        if vl>60:
            vl=60
        if vl<=0:
            vl=3
        db[col].update_one({'_id': item['_id']}, {'$set': {'video_feature.editing': {'video_len':vl}}})

